//
//  OptionView.m
//  eye_hunter
//
//  Created by cherry on 10-10-10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "ScrollView.h"
#import "HunterConst.h"
@implementation ScrollView

@synthesize targets;
@synthesize stateButtons;
@synthesize centerIndex;

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        [self setUserInteractionEnabled:YES];
    }
    return self;
}
- (void)initButtons{
    stateButtons = [[NSMutableArray alloc] init];
    for (int i = 0; i<BTN_NUM; i++) {
        CGFloat BTN_SIZE = NORMAL_BTN_SIZE;
        if (i == 3) {
            BTN_SIZE = CENTER_BTN_SIZE; 
        }        
        StateButtonView *stateButton = [[StateButtonView alloc] initWithFrame:BTN_FRAME[i]];
        PicTarget *target = (PicTarget *)[targets objectAtIndex:i];
        if (target.isFound) {
            stateButton.image = [UIImage imageNamed:[target dustPic]];
        }else {
            stateButton.image = [UIImage imageNamed:[target lightPic]];
        }
        stateButton.posIndex = i;
        stateButton.targetIndex = i;
        stateButton.targets = self.targets;
        [self addSubview:stateButton];
        [stateButtons addObject:stateButton];
        [stateButton release];
    }
    self.centerIndex = 3;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    oldPosision = [[touches anyObject] locationInView:self];
}

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    CGPoint pt = [[touches anyObject] locationInView:self];
    CGFloat dist = oldPosision.x - pt.x;
    if (dist > MIN_X_DISTANCE) {
        [self scrollLeft];
    }else if (dist < (-1) * MIN_X_DISTANCE) {
        [self scrollRight];
    }
}

-(void) scrollLeft{
    self.centerIndex += 1;
    if (centerIndex >= BTN_NUM) {
        centerIndex -= BTN_NUM;
    }
    for (int i=0; i<BTN_NUM; i++) {
        StateButtonView *stateBtn = [stateButtons objectAtIndex:i];
        [stateBtn scrollLeft];
    }
}
-(void) scrollRight{
    self.centerIndex -= 1;
    if (centerIndex < 0) {
        centerIndex += BTN_NUM;
    }
    for (int i=0; i<BTN_NUM; i++) {
        StateButtonView *stateBtn = [stateButtons objectAtIndex:i];
        [stateBtn scrollRight];
    }
}

-(Boolean) foundTarget: (int) targetId{
    StateButtonView *centerBtnView = (StateButtonView *)[self.stateButtons objectAtIndex:centerIndex];
    return [centerBtnView foundTarget:targetId];
}

- (void)dealloc {
    [super dealloc];
}

@end
